<?php


namespace App\Remote\Feishu\Message;

use Exception;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpFoundation\Request as RequestAlias;


class FeishuAuthRemote extends FeishuRemote
{

    /**
     * 获取飞书企业token
     *
     * @return mixed|void
     * @throws Exception
     */
    public function getAccessToken()
    {
        try {
            $getAccessTokenFunc = function () {
                $response = self::customRequest(
                    "/open-apis/auth/v3/tenant_access_token/internal",
                    RequestAlias::METHOD_POST,
                    [
                        'app_id'     => $this->appId,
                        'app_secret' => $this->appSecret
                    ]
                );
                return sprintf("Bearer %s", $response['tenant_access_token']);
            };

            return Cache::remember(
                "ACCESS_TOKEN:appId:" . $this->appId,
                60 * 30,
                $getAccessTokenFunc
            );
        } catch (Exception $exception) {
            handleException($exception);
        }
    }


}
